 layui.use(['table', 'form', 'jquery', 'common', 'laydate'], function() {
      let table = layui.table;
      let form = layui.form;
      let $ = layui.jquery;
      let common = layui.common;
      let laydate = layui.laydate;

      let cols = [
        [{
          field: "id",
          title: "ID",
          unresize: "true",
          align: "center"
        }, {
          field: 'name',
          title: '插件名称',
          width: 200,
          align: 'center',
        }, {
          field: "url",
          title: "下载地址",
          unresize: "true",
          align: "center"
        }, {
          field: "readme",
          title: "插件介绍",
          unresize: "true",
          align: "center"
        }, {
          field: "addtime",
          title: "创建时间",
          unresize: "true",
          align: "center"
        }, {
          title: '操作',
          toolbar: '#options',
          unresize: true,
          align: 'center',
          width: 180,
        }]
      ]

      table.render({
        elem: '#news',
        id: 'news',
        url: './ajax1.php?act=downgetDataList',
        page: true,
        cols: cols,
        cellMinWidth: 100,
        skin: 'line',
        toolbar: '#toolbar',
        defaultToolbar: [{
          title: '刷新',
          layEvent: 'refresh',
          icon: 'layui-icon-refresh',
        }, 'filter', 'print', 'exports']
      });

      table.on('tool(news)', function(obj) {
        if (obj.event === 'remove') {
          window.remove(obj);
        } else if (obj.event === 'edit') {
          window.edit(obj);
        }
      });

      table.on('toolbar(news)', function(obj) {
        if (obj.event === 'add') {
          window.add();
        } else if (obj.event === 'refresh') {
          window.refresh();
        }
      });


      // 修改状态
      form.on("switch(status)", function(data) {
        var id = this.value;
        var load = layer.load();
        $.post("?a=status", {
          id: id
        }, function(res) {
          layer.close(load);
          if (res.code == 200) {
            layer.msg(res.msg, {
              icon: 1,
              time: 1500
            })
          } else {
            layer.msg(res.msg, {
              icon: 2,
              time: 1500
            }, function() {
              $(data.elem).prop("checked", !$(data.elem).prop("checked"));
              form.render()
            })
          }
        }, "json")
      });
      //弹出窗设置 自己设置弹出百分比
      function screen() {
        if (typeof width !== 'number' || width === 0) {
          width = $(window).width() * 0.8;
        }
        if (typeof height !== 'number' || height === 0) {
          height = $(window).height() * 0.5;
        }
        return [width + 'px', height + 'px'];
      }

      // 添加数据
      window.add = function() {
        layer.open({
          type: 2,
          maxmin: true,
          title: '新增公告管理',
          shade: 0.1,
          area: screen(),
          content: "./down?my=add"
        });
      }

      // 发送添加数据内容
      form.on('submit(addSave)', function(data) {
        $.ajax({
          data: JSON.stringify(data.field),
          url: "./ajax1.php?act=add_down",
          contentType: 'application/json',
          dataType: 'json',
          type: 'post',
          success: function(res) {
            if (res.code == 200) {
              layer.msg(res.msg, {
                icon: 1,
                time: 1000
              }, function() {
                parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                parent.layui.table.reload("news");
              });
            } else {
              layer.msg(res.msg, {
                icon: 2,
                time: 1000
              });
            }
          }
        })
        return false;
      });

      // 修改数据
      window.edit = function(obj) {
        layer.open({
          type: 2,
          maxmin: true,
          title: '修改公告管理',
          shade: 0.1,
          area: screen(),
          content: './down?my=edit&id=' + obj.data['id']
        });
      }

      // 发送修改数据内容
      form.on('submit(editSave)', function(data) {
        $.ajax({
          data: JSON.stringify(data.field),
          url: "./ajax1.php?act=edit_down",
          contentType: 'application/json',
          dataType: 'json',
          type: 'post',
          success: function(res) {
            if (res.code == 200) {
              layer.msg(res.msg, {
                icon: 1,
                time: 1000
              }, function() {
                parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                parent.layui.table.reload("news");
              });
            } else {
              layer.msg(res.msg, {
                icon: 2,
                time: 1000
              });
            }
          }
        })
        return false;
      });

      // 数据删除
      window.remove = function(obj) {
        layer.confirm('确定要删除该插件', {
          icon: 3,
          title: '提示'
        }, function(index) {
          layer.close(index);
          let loading = layer.load();
          $.ajax({
            url: './ajax.php',
            data: {
              act:"downremove",
              id: obj.data['id']
            },
            dataType: 'json',
            type: 'POST',
            success: function(res) {
              layer.close(loading);
              //判断有没有权限
              if (res && res.code == 999) {
                layer.msg(res.msg, {
                  icon: 5,
                  time: 2000,
                })
                return false;
              } else if (res.code == 200) {
                layer.msg(res.msg, {
                  icon: 1,
                  time: 1000
                }, function() {
                  obj.del();
                });
              } else {
                layer.msg(res.msg, {
                  icon: 2,
                  time: 1000
                });
              }
            }
          })
        });
      }

      window.refresh = function(param) {
        table.reload('news');
      }
    })